package com.lnsf.bookstore.system.vipuser.controller;

import java.util.*;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.annotation.SysLog;
import com.lnsf.bookstore.common.dto.ResultSuccessDto;
import com.lnsf.bookstore.common.util.excel.ExcelExport;
import com.lnsf.bookstore.system.vipuser.service.VipUserService;
import com.lnsf.bookstore.system.vipuser.vo.VipUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import com.lnsf.bookstore.system.user.service.UserService;
import com.lnsf.bookstore.system.user.vo.User;

@Controller
@RequestMapping("/vipuser")
public class VipUserController {

	@Autowired
	private VipUserService vipUserService;

	@RequestMapping("/v_list.do")
	public String list(HttpServletRequest request, ModelMap modelMap){
		return "admin/vipuser/vipuser";
	}


	@RequestMapping("/info/{userId}")
	@ResponseBody
	public Object info(ModelMap modelMap, HttpServletRequest request, @PathVariable("userId") Integer userId){
	/*	VipUser vipuser = sessionProvider.getUser();*/
		Map<String,Object> map = new HashMap<String, Object>();
		VipUser vipUser = vipUserService.selectById(userId);
		map.put("vipuser",vipUser);
		return  map;
	}

	/**
	 * 查看所有列表
	 */
	@RequestMapping("/queryAll")
	@ResponseBody
	public Object queryAll(@RequestParam Map<String, Object> params, VipUser vipUser) {
		PageInfo<VipUser> pageInfo = vipUserService.selectAllVip(params,vipUser);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page",pageInfo);
		return map;
	}

	/**
	 * 修改、新增操作
	 */
	@SysLog("修改，新增会员")
	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
	@ResponseBody
	public Object saveOrUpdate(HttpServletRequest request, Integer id , @RequestBody  VipUser vipUser, ModelMap modelMap){
		if(vipUser.getId()==null){//新增
			//verifyForm(permission);
			this.salt(vipUser);//加盐加密
			vipUserService.saveVipUser(vipUser);
			//rolePermissionService.saveOrUpdate(role);
		}else{//修改
			//verifyForm(permission);
			vipUserService.updateVipUser(vipUser);
			//rolePermissionService.saveOrUpdate(role);
		}
		return new ResultSuccessDto("操作成功");
	}


	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	//@RequiresPermissions("user:delete")
	public Object delete(@RequestBody Integer[] ids) {
		vipUserService.deleteBatch(ids);

		return new ResultSuccessDto("操作成功");
	}


	/**
	 * 导出会员
	 */
	@RequestMapping("/export")
	@ResponseBody
	/*@RequiresPermissions("vipuser:export")*/
	public Object export(@RequestParam Map<String, Object> params, HttpServletResponse response, VipUser vipUser) {
		PageInfo<VipUser> pageInfo = vipUserService.selectAllVip(params,vipUser);

		ExcelExport ee = new ExcelExport("会员列表");

		String[] header = new String[]{"会员名称", "性别", "会员级别", "手机号码"};

		List<Map<String, Object>> list = new ArrayList<>();

		if (pageInfo.getList() != null && pageInfo.getList().size() != 0) {
			for (VipUser userInfo : pageInfo.getList()) {
				LinkedHashMap<String, Object> map = new LinkedHashMap<>();
				map.put("USERNAME", userInfo.getUsername());
				map.put("GENDER", userInfo.getGender());
				map.put("LEVEL_NAME", "");
				map.put("MOBILE", userInfo.getMoney());
				list.add(map);
			}
		}

		ee.addSheetByMap("会员", list, header);
		ee.export(response);

		return  new ResultSuccessDto("导出成功");
	}

	/**
	 * 初始密码是123456
	 * @param vipUser
	 */
	private void salt(VipUser vipUser){
		String salt = UUID.randomUUID().toString().replaceAll("-", "");//盐
		int hashIterations = 15;//迭代次数
		System.out.println(salt);
		SimpleHash simpleHash = new SimpleHash("md5", "123456", salt, hashIterations);
		vipUser.setPassword(simpleHash.toString());
	}
	
}
